//
//  KKKK.m
//  LawyerClient
//
//  Created by xujingjing on 16/1/12.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "QHTools.h"
//#import "MBProgressHUD.h"
//#import "FMDB.h"
#import "UIView+FindNav.h"
@implementation QHTools
+(BOOL)isEmpty:(NSString *)str{
    
    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }

}
+ (BOOL)isBlankString:(NSString *)string{
    
     if (string == nil) {
        
         return YES;
        
        }
    
     if (string == NULL) {
        
         return YES;
        
         }
         if ([string isKindOfClass:[NSNull class]]) {
        
         return YES;
        
         }
    
//     if ([[string stringByTrimmingCharactersInSet:[kCFCharacterSetWhitespaceAndNewline]] length]==0) {
//        
//         returnYES;
//        
//         }
//    
     return NO;
    
}

+(BOOL)checkTel:(NSString *)str{
   
    
    NSString *regex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    if (!isMatch) {
       // if ([NSBundle mainBundle] >= 8.0) {
        
        
//        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提醒" message:@"请输入正确的手机号码" preferredStyle:UIAlertControllerStyleAlert];
//        
//        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            [alertC dismissViewControllerAnimated:NO completion:nil];
//        }];
//        [alertC addAction:sure];
     //   [self presentViewController:alc animated:YES completion:nil];

        
        
//        }else{
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

//        }
        return NO;
    }
    
    return YES;
}

+(BOOL)checkTelNoAlert:(NSString *)phoneNum{
    NSString *regex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:phoneNum];
    if (!isMatch) {
        return NO;
    }
    return YES;

}
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}
#pragma mark车牌号正则
+(BOOL)checkCarNum:(NSString *)carNumber isShowAlert:(BOOL)isShow{
    NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];

    BOOL isMatch = [carTest evaluateWithObject:carNumber];
    if (!isMatch) {
        if (isShow) {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请正确输入车牌号" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        return NO;
    }
    return YES;

}
+(void)AlertControl:(NSString *)message{

    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提醒" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
   
}

//字体大小 颜色 范围
+(void)fuwenbenLabel:(UILabel *)labell FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:labell.text];
    [str addAttribute:NSFontAttributeName value:font range:range];
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    labell.attributedText = str;
    
}


#define kFontSize 13
+( CGFloat )heightForText:( NSString *)text

{
    
    // 设置计算文本时字体的大小 , 以什么标准来计算
    
    NSDictionary *attrbute = @{ NSFontAttributeName :[ UIFont systemFontOfSize : kFontSize ] } ;
    
    return [text boundingRectWithSize : CGSizeMake ( kWindowWidth , 1000 ) options :( NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin ) attributes :attrbute context : nil ]. size . height ;
    
}

+ ( CGFloat )heightForImage:( UIImage *)image

{
    
    //(1) 获取图片的大小
    
    CGSize size = image. size ;
    
    //(2) 求出缩放比例
    
    CGFloat scale = kWindowWidth / size. width ;
    
    CGFloat imageHeight = size. height * scale;
    
    return imageHeight;
    
}


+ (NSString *)stringFromDate:(NSDate *)date{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYYMMdd"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}

//获取当前日期 返回当前日期的字符串
+(NSString *)currentDate:(NSDate *)date {
    
  //  NSDate *date = [NSDate date];//获取当前日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY年-MM月-dd日"];//转换日期格式
    NSString *showtimeNew = [formatter stringFromDate:date];//将日期提取成NSString
    return showtimeNew;
}

//date->string
+(NSString *)changeTOStringWithDate:(NSString *)string{
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[string integerValue]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    
    return [formatter stringFromDate:confromTimesp];
    
}

//时间字符串转成时间戳
+(NSString *)timeInterverStringFromTimeString:(NSString *)string{
    
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate* date = [formatter dateFromString:string];
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
//    NSLog(@"timeSp:%@",timeSp); //时间戳的值
    return timeSp;
}
//时间戳转时间
+(NSString *)stringFromTimeInterVerString:(NSString *)timeIntervel{
  NSDate *confromTimesp =  [NSDate dateWithTimeIntervalSince1970:[timeIntervel floatValue]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
 NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

    
    return confromTimespStr;
}
//含有时分秒的时间戳转时间
+(NSString *)stringWithHHMMSSFromTimeInterVerString:(NSString *)timeIntervel{
    
 
    NSTimeInterval time=[timeIntervel doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter stringFromDate: detaildate];
}
//处理特殊字符url
+(NSString *)dealWithInnomalString:(NSString *)urlString{

    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)urlString ,NULL ,CFSTR("!*'();:@&=+$,/?%#[]") ,kCFStringEncodingUTF8));
   return result;


}
//打电话
+(void)callWithTel:(NSString *)phoneNum{
    
    NSString *number = phoneNum;// 此处读入电话号码
    
    //  NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number]; //number为号码字符串 如果使用这个方法 结束电话之后会进入联系人列表
    
    
    NSString *num = [[NSString alloc] initWithFormat:@"telprompt://%@",number]; //而这个方法则打电话前先弹框  是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号
    
    
}


+(NSString *)timeInterverString{

return  [NSString stringWithFormat:@"%f",[[NSDate dateWithTimeIntervalSinceNow:0]timeIntervalSince1970]];
}



@end
